package com.springcloud.authentication.common;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class RestResult {
	private static final  Logger LOGGER = LogManager.getLogger(RestResult.class);

	/**
	 * normal
	 * @param success
	 * @param data
	 * @param message
	 * @param <T>
	 * @return
	 */
	private static <T> RestResultEntity<T> genResult(boolean success, T data,String code,String message) {
		RestResultEntity<T> result = RestResultEntity.newInstance();
		result.setResult(success);
		result.setCode(code);
		result.setData(data);
		result.setMessage(message);
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("generate rest result:{}", result);
		}
		return result;
	}

	/**
	 * success
	 * @param data
	 * @param <T>
	 * @return
	 */
	public static <T> RestResultEntity<T> genResult(T data) {
		return genResult(true, data,"200", null);
	}
	
	
	/**
	 * success no message
	 * @return
	 */
	public static RestResultEntity<?> genResult() {
		return genResult(null);
	}
	

	/**
	 * error message
	 * @param message error message
	 * @param <T>
	 * @return
	 */
	public static <T> RestResultEntity<T> genErrorResult(String message) {
		return genResult(false, null, "500", message);
	}
	
	
	/**
	 * error
	 * @param error error code,message
	 * @param <T>
	 * @return
	 */
	public static <T> RestResultEntity<T> genErrorResult(String code,String message) {
		return genResult(false, null,code, message);
	}

	

}
